package com.bk.service.impl;

import com.bk.context.BaseContext;
import com.bk.entity.*;
import com.bk.mapper.*;
import com.bk.result.PageResult;
import com.bk.service.PersonalCenterService;
import com.bk.vo.LookHistoryVO;
import com.bk.vo.MessageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonalCenterServiceImpl implements PersonalCenterService {
    //用户
    @Autowired
    private UserMapper userMapper;
    //帖子
    @Autowired
    private InnovationMapper innovationMapper;
    //点赞
    @Autowired
    private LikeMapper likeMapper;
    //浏览
    @Autowired
    private LookMapper lookMapper;
    //收藏
    @Autowired
    private PreserveMapper preserveMapper;
    //评论
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageResult messagePage(Integer page,Integer pageSize) {
        //1.获取我的userid
        Long currentId = BaseContext.getCurrentId();
        //2.查询所有我的帖子,将id封装到集合
        List<Innovation> myInnovations = innovationMapper.getByUserId(currentId);
        //3.遍历集合，每一个帖子id都遍历点赞，评论，收藏表，将相应数据封装到MessageVO,得到MessageVO的List集合
        List<MessageVO> messages = new ArrayList<>();
        for(Innovation innovation: myInnovations)
        {
            //获取帖子id
            Long innoId = innovation.getId();
            //查询点赞记录
            List<Like> likes = likeMapper.getByInnovationId(innoId);
            //遍历点赞记录之后封装
            for(Like like:likes)
            {
                MessageVO messageVO = new MessageVO();
                messageVO.setInnovationId(innoId);
                //获取点赞人
                Long userId = like.getUserId();
                String name = userMapper.getUsername(userId);
                messageVO.setName(name);
                messageVO.setType("点赞");
                messageVO.setCreateTime(like.getCreateTime());
                messages.add(messageVO);
            }
            //遍历收藏记录封装
            List<Preserve> preserves = preserveMapper.getByInnovationId(innoId);
            for(Preserve preserve:preserves)
            {
                MessageVO messageVO = new MessageVO();
                messageVO.setInnovationId(innoId);
                //获取收藏人
                Long userId = preserve.getUserId();
                String name = userMapper.getUsername(userId);
                messageVO.setName(name);
                messageVO.setType("收藏");
                messageVO.setCreateTime(preserve.getCreateTime());
                messages.add(messageVO);
            }
            //遍历浏览记录之后封装
            List<Look> looks = lookMapper.getByInnovationId(innoId);
            for(Look look:looks)
            {
                MessageVO messageVO = new MessageVO();
                messageVO.setInnovationId(innoId);
                //获取浏览人
                Long userId = look.getUserId();
                String name = userMapper.getUsername(userId);
                messageVO.setName(name);
                messageVO.setType("浏览");
                messageVO.setCreateTime(look.getCreateTime());
                messages.add(messageVO);
            }
            //遍历评论表之后封装
            List<Comment> comments = commentMapper.getByInnovationId(innoId);
            for(Comment comment:comments)
            {
                MessageVO messageVO = new MessageVO();
                messageVO.setInnovationId(innoId);
                //获取浏览人
                Long userId = comment.getUserId();
                String name = userMapper.getUsername(userId);
                messageVO.setName(name);
                messageVO.setType("评论");
                messageVO.setCreateTime(comment.getCreateTime());
                messages.add(messageVO);
            }
        }
        //4.将结果集按照时间顺序排序
        messages.sort((o1, o2) -> o1.getCreateTime().isAfter(o2.getCreateTime()) ? -1 : 1);
        //5.分页，返回结果
        int size = messages.size();
        int start = (page-1)*pageSize;
        int end = (start+pageSize)<size?start+pageSize:size;
        List<MessageVO> messageVOS = messages.subList(start, end);
        return new PageResult(messageVOS.size(),messageVOS);
    }

    /**
     * 浏览记录分页
     * @return
     */
    @Override
    public PageResult historyPage(Integer page, Integer pageSize) {
        //0.开启分页
        PageHelper.startPage(page,pageSize);
        //1.获取userid
        Long userId = BaseContext.getCurrentId();
        //多表联查获取返回数据
        Page<LookHistoryVO> historyPage= preserveMapper.historyPage(userId);
        List<LookHistoryVO> result = historyPage.getResult();
        for (LookHistoryVO lookHistoryVO : result) {
            String authorName = userMapper.getUserNameById(userId);
            lookHistoryVO.setAuthorName(authorName);
        }
        return new PageResult(historyPage.getTotal(),historyPage.getResult());

    }

    /**
     * 收藏夹分页
     * @return
     */
    @Override
    public PageResult preservePage() {
        //0.开启分页

        //1.获取userid

        //2.查看收藏表，得到帖子id,将时间封装到MyPreserveVo

        //3.查询帖子id，将对应信息封装

        //4.获得集合，按照时间排序返回
        return null;
    }

    /**
     * 查看已发布的文章
     * @return
     */
    @Override
    public PageResult innovationPage() {
        //0.开启分页

        //1.获取userid

        //2.根据id查询帖子集合

        //3.遍历集合，根据查询到的作者id封装作者名称与头像

        //4.查询帖子id相关的点赞，评论，收藏，浏览数

        //5.封装结果(InnovationVO)
        return null;
    }


}
